package com.meiqiongproperty.yiguanjia.ui.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.meiqiongproperty.yiguanjia.R;

/**
 * Created by Administrator on 2017/4/30.
 */

public class ProductDetailBuyPopupWindow extends PopupWindow {
    private Activity activity;
    private Context context;
    private View view;
    RelativeLayout decrease, increase;
    ImageView close;
    EditText et_amount;


    public ProductDetailBuyPopupWindow(final Activity context) {
        super(context);
        this.activity = context;
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.product_detail_buy_pop_layout, null);
        close = (ImageView) view.findViewById(R.id.close);
        decrease = (RelativeLayout) view.findViewById(R.id.decrease);
        increase = (RelativeLayout) view.findViewById(R.id.increase);
        et_amount = (EditText) view.findViewById(R.id.et_amount);

        et_amount.setEnabled(false);
        et_amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int amount = Integer.parseInt(s.toString());
                if (amount>99){
                    et_amount.setText((amount-1)+"");
                }
            }
        });

        CharSequence text = et_amount.getText();
        //Debug.asserts(text instanceof Spannable);
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }

        int height = context.getWindowManager().getDefaultDisplay().getHeight();
        int width = context.getWindowManager().getDefaultDisplay().getWidth();

        //CommonUtil.showToast("高度:"+height+"宽度:"+width);
        // 设置按钮监听
        // 设置SelectPicPopupWindow的View
        this.setContentView(view);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.shoppingcartstyle);

        this.setOutsideTouchable(true);

//        view.setOnTouchListener(new View.OnTouchListener() {
//
//            public boolean onTouch(View v, MotionEvent event) {
//                int height = view.getMeasuredHeight();
//
//                int y = (int) event.getY();
//
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    if (y >= height) {
//                        dismiss();
//                        Toast.makeText(context,"height===>"+height+"------"+y,0).show();
//                    }
//                }
//                return true;
//            }
//        });
    }
}
